﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OAProject.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OAProject.Domain.Entities;
using Microsoft.AspNetCore.Authorization;


namespace OAProject.API.Controllers
{
    /// <summary>
    /// 角色Api控制器
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize]     //全局授权过滤器
    public class RolesAPIController : ControllerBase
    {
        //依赖注入
        private IRolesRepository _repository;   //角色
        private IMenusRepository _Menus;        //菜单
        private IRoleMenusRepository _RoleMenus;    //角色菜单
        public RolesAPIController(IRolesRepository repository, IMenusRepository Menus, IRoleMenusRepository RoleMenus)
        {
            _repository = repository;
            _Menus = Menus;
            _RoleMenus = RoleMenus;
        }

        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShow(string roleName = "", int page = 1, int limit = 10)
        {
            List<Roles> roles = null;
            if (roleName == "")
            {
                roles = _repository.Show();
            }
            if (roleName!=null)
            {
                roles = _repository.Show().Where(x => x.RoleName.Contains(roleName)).ToList();          //BUG
                var list = roles.Skip((page - 1) * limit).Take(limit).ToList();
                return Ok(new
                {
                    data = list,
                    code = 0,
                    msg = "",
                    count = roles.Count
                });
            }
            else
            {
                roles = _repository.Show();
                var list= roles.Skip((page - 1) * limit).Take(limit).ToList();
                return Ok(new
                {
                    data = list,
                    code = 0,
                    msg = "",
                    count = roles.Count
                });
            }
            
        }

        /// <summary>
        /// 角色表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete(int id)
        {
            var list = _repository.Delete(id);
            return Ok(new
            {
                data = list
            });
        }

        /// <summary>
        /// 角色的添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromForm] Roles obj)   //[FromForm]是接受页面传过来的数据
        {
            obj.CreateDate = DateTime.Now;  //创建当前时间
            obj.CreateName = "张三";         //修改时登录人的姓名
            int a = _repository.Insert(obj);
            return Ok(a);
        }

        /// <summary>
        /// 角色的反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Fantian(int id)
        {
            var list = _repository.GetById(id);
            return Ok(new
            {
                data = list
            });
        }

        /// <summary>
        /// 角色的修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Update([FromForm] Roles obj)
        {
            obj.CreateDate = DateTime.Now;      //创建当前时间
            obj.CreateName = "张三";             //修改时登录人的姓名
            var list = _repository.Update(obj);
            return Ok(list);
        }

        #region 角色菜单树形
        ///// <summary>
        ///// 父类的哈希表(递归)
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public IActionResult Shu(int roleID)
        //{
        //    List<int> intlist = new List<int>();
        //    List<Menus> menus = _Menus.Show();
        //    List<Menus> menus1 = menus.Where(x => x.ParentID.Equals(0)).ToList();
        //    List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
        //    foreach (var item in menus1)
        //    {
        //        Dictionary<string, object> json = new Dictionary<string, object>();
        //        json.Add("id", item.MenuID);
        //        json.Add("title", item.MenuName);
        //        json.Add("spread", true);
        //        Shu_nest(menus, json, item.MenuID);
        //        data.Add(json);
        //    }
        //    return Ok(data);
        //}

        ///// <summary>
        ///// 子类
        ///// </summary>
        ///// <param name="menus"></param>
        ///// <param name="json"></param>
        ///// <param name="menuID"></param>
        //private void Shu_nest(List<Menus> menus, Dictionary<string, object> json, int menuID)
        //{
        //    List<Menus> menus1 = menus.Where(x => x.ParentID.Equals(menuID)).ToList();
        //    List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
        //    if (menus1.Count == 0)
        //    {
        //        json.Add("children", null);
        //        return;
        //    }
        //    foreach (var item in menus1)
        //    {
        //        Dictionary<string, object> json1 = new Dictionary<string, object>();
        //        json1.Add("id", item.MenuID);
        //        json1.Add("title", item.MenuName);
        //        json1.Add("spread", true);
        //        Shu_nest(menus, json1, item.MenuID);
        //        data.Add(json1);
        //    }
        //    json.Add("children", data);
        //}

        /// <summary>
        /// 拼接字符串的树
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShuShow(int id)
        {
            List<RoleMenus> m = _RoleMenus.RoleMenusShow(id);
            List<int> ids = m.Where(x => x.MenuID1 != 0).Select(y => y.MenuID).ToList();
            return Ok(new
            {
                data = m,
                code = 0,
                msg = "",
                checkedId = ids
            });
        }

        /// <summary>
        /// 修改分配权限
        /// </summary>
        /// <param name="menuIds"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleMenus([FromForm]List<int> menuIds, [FromForm] int roleId)
        {
            var list = _RoleMenus.AddRoleMenus(menuIds, roleId);
            return Ok(list);
        }
        #endregion
    }
}
